var express = require('express');
var router = express.Router();
var pool = require('../db/sql.js');

// 商品列表
router.get('/list', async (req, res) => {
	const { pageNo = 1, pageSize = 10, searchKey = '' } = req.query;
	console.log('searchKey===', searchKey)
    try {
		const [totalRes] = await pool.execute('SELECT COUNT(*) AS total FROM goodsList WHERE name LIKE ?', [`%${searchKey}%`]);
        const [rows] = await pool.execute(`SELECT * FROM goodsList LIMIT ${(pageNo - 1) * pageSize}, ${pageSize}`);
        res.json({
            code: 200,
            data: {
				list: rows,
				total: totalRes[0].total
			},
            message: 'success'
		});
    } catch (err) {
        console.error(err);
        res.status(500).send('Database error');
    }
});


module.exports = router;
